import ChartBase from './chart-base'
import { deepClone, mergeObject } from '@/utils'

export default class ProcessTopology extends ChartBase {
  constructor() {
    super()
  }

  // 基础描述
  _info = {
    name: '流程拓扑图',
    type: 'process-topology',// 图表类型
    group: 'topology',// 图表分类
    component: 'process-topology',
    icon: 'process-topology'// 图表展示ICON
  }

  // 总配置项
  _defaultOption = {
    config: {
      pagination: {
        show: true
      }
    },
    style: {}
  }
  // 系列
  _defaultSeries = {}

  // 数据结构描述
  _dataPoolConfig = [
    {
      label: '主键字段',
      key: 'keyId',
      type: ['string', 'number'],
      description: '字段类型为「 数字类型 」或「 字符类型 」',
      limit: 1
    },
    {
      label: '关系字段',
      key: 'relation',
      type: ['string', 'date', 'number'],
      description: '用于展示节点之间的关系, 字段类型为「 数字类型 」或「 字符类型 」',
      limit: 1
    },
    {
      label: '节点名称',
      key: 'node',
      type: ['string', 'date', 'number'],
      description: '关系图节点的名称字段, 字段类型为「 数字类型 」或「 字符类型 」',
      limit: 1
    }
  ]

  // 样式配置描述信息
  // _styleOptionConfig = [
  //   {
  //     label: '颜色选择器',
  //     bindKeys: 'key',
  //     controlType: 'color-picker',
  //     controlValue: '#000000',
  //     controlProps: {},
  //     controlConfigs: {}
  //   }
  // ]


  getOption({ dataList = [], dataPoolField, styleOptionConfig = [] }) {

    let defaultOption = deepClone(this._defaultOption)

    let option = this._transformStyleOptionConfig(styleOptionConfig)
    const checkHasValue = array => array && array instanceof Array && array.length > 0
    if (checkHasValue(dataPoolField['keyId']) && checkHasValue(dataPoolField['relation']) && checkHasValue(dataPoolField['node'])) {
      const idField = dataPoolField['keyId'][0].fieldName
      const nameField = dataPoolField['node'][0].fieldName
      const parentField = dataPoolField['relation'][0].fieldName
      let listData = []
      listData = dataList.reduce((r, c) => {
        r.push({
          id: `${c[idField]}`,
          label: `${c[nameField]}`,
          parent_id: `${c[parentField]}`,
        })
        return r
      }, [])
      let map = {};
      listData.forEach((item) => {
        map[item.id] = item;
      });
      let list = [];
      listData.forEach((item) => {
        const parent = map[item.parent_id];
        if (parent) {
          (parent.children || (parent.children = [])).push(item);
        } else {
          list.push(item);
        }
        return list
      });

      defaultOption.treeData = list
    }
    if (defaultOption && option) defaultOption = mergeObject(defaultOption, option)
    return defaultOption
  }
}
